package br.roove.bomotivo.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import br.roove.bomotivo.model.Item;
import br.roove.bomotivo.util.JSONParser;

public class ListEvents extends ListFragment{
	
	SharedPreferences.Editor prefs_editor;
	SharedPreferences prefs;
	private ListView listview;
	String MY_PREFS = "eventospref";
	private CustomArrayAdapter mAdapter;
	private ArrayList<Item> itens;
	private JSONParser jsonParser;
	private String URL = "http://bomotivo.roove.com.br/service/eventos.json";
	private String BASE_URL = "http://bomotivo.roove.com.br";
	ProgressDialog pd;
	//private String nameHeader, valueHeader;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		prefs_editor = this.getActivity().getSharedPreferences(MY_PREFS, 0).edit();
		prefs = this.getActivity().getSharedPreferences(MY_PREFS, 0);
		

		View rootView = inflater.inflate(R.layout.list_view_events, container, false);
		listview = (ListView)rootView.findViewById(android.R.id.list);
		jsonParser  = new JSONParser(getActivity());
		return rootView;
	}
	
	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
//		String json = jsonParser.getJSONString(URL);
//		if(json != "erro" && json != null){
		
		pd = new ProgressDialog(getActivity());
		
		
		AsyncHttpClient asyncClient = new AsyncHttpClient();
		asyncClient.get(URL, null, new JsonHttpResponseHandler(){
			
			
			

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				pd = ProgressDialog.show(getActivity(), null, "Buscando...");
				//pd.show(getActivity(), null, "Buscando...", false, true);
			}

			@Override
			public void onSuccess(JSONArray array) {
				// TODO Auto-generated method stub
				
				super.onSuccess(array);
				pd.dismiss();
				itens = new ArrayList<Item>();
			JSONArray jsonArray = new JSONArray();
			jsonArray = array;
			for (int i = 0; i < jsonArray.length(); i++){
				Item item = new Item();
				JSONObject jsonObject;
				try {
					jsonObject = jsonArray.getJSONObject(i);
				
				item.setId(jsonObject.getInt("id"));
				item.setData(jsonObject.getString("data"));
				item.setEvento(jsonObject.getString("nome"));
				item.setInfo(jsonObject.getString("info"));
				item.setNumero_de_pessoas(jsonObject.getInt("participantes"));
				item.setValor(jsonObject.getInt("valor"));
				item.setURL_foto_evento(BASE_URL + jsonObject.getString("thumb_original"));
				item.setURL_foto_lista(BASE_URL + jsonObject.getString("thumb_reduzido"));
				itens.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		//}
		
		
		//itens = new ArrayList<Item>();
//		Item item = new Item();
//		item.setData("23/07/2013");
//		item.setEvento("Baladinha");
//		item.setNumero_de_pessoas(123);
//		item.setValor(5);
//		itens.add(item);
		
		mAdapter = new CustomArrayAdapter(getActivity(), android.R.id.list, itens);
		listview.setAdapter(mAdapter);
			}
			}
			
		});
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		TextView view = (TextView)v.findViewById(R.id.qtd_pessoas_simple_item);
		
		Bundle bundle = new Bundle();
		bundle.putString("balada", itens.get(position).getEvento());
		bundle.putString("data", itens.get(position).getData());
		bundle.putString("valor", Integer.toString(itens.get(position).getValor()));
		//bundle.putString("qtd_pessoas", Integer.toString(itens.get(position).getNumero_de_pessoas()));
		bundle.putString("qtd_pessoas", view.getText().toString());
		bundle.putString("url_lista",itens.get(position).getURL_foto_lista());
		bundle.putString("url_evento", itens.get(position).getURL_foto_evento());
		bundle.putString("info", itens.get(position).getInfo());
		bundle.putInt("id", itens.get(position).getId());
		
		ItemSelected item = ItemSelected.newInstance(bundle);
		getFragmentManager().beginTransaction().replace(R.id.fragment_main, item).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).addToBackStack("item_selected").commit();
	}
	

}
